package com.itlxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itlxd.entity.DishFlavor;
import com.itlxd.mapper.DishFlavorMapper;
import com.itlxd.service.IDishFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品口味关系表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class DishFlavorServiceImpl implements IDishFlavorService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Integer insertBatch(List<DishFlavor> flavors) {
        int count = 0;
        for (DishFlavor flavor : flavors) {
            dishFlavorMapper.insert(flavor);
            count++;
        }
        return count;
    }

    @Override
    public List<DishFlavor> getByDishId(Long dishId) {
        LambdaQueryWrapper<DishFlavor> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(DishFlavor::getDishId, dishId)
                .orderByDesc(DishFlavor::getUpdateTime);
        return dishFlavorMapper.selectList(lambdaQuery);
    }

    @Override
    public Integer updateById(DishFlavor dishFlavor) {
        return dishFlavorMapper.updateById(dishFlavor);
    }

    @Override
    public Integer deleteBatchByDishId(Long[] dishIds) {
        LambdaQueryWrapper<DishFlavor> lambdaQuery = new LambdaQueryWrapper<>();
        int count = 0;
        for (Long dishId : dishIds) {
            lambdaQuery.eq(DishFlavor::getDishId, dishId);
            dishFlavorMapper.delete(lambdaQuery);
            count++;
        }

        return count;
    }

    @Override
    public Integer insertFlavor(DishFlavor dishFlavor) {
        return dishFlavorMapper.insert(dishFlavor);
    }
}
